<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="分类">
        <category-tree-cascader @category-lowest-level-change="updateCategory"></category-tree-cascader>
      </el-form-item>
      <el-form-item label="品牌">
        <el-select v-model="dataForm.brandId" placeholder="请选择">
          <el-option
            v-for="item in brandList"
            :key="item.brandId"
            :label="item.name"
            :value="item.brandId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="spu">
        <el-select v-model="dataForm.spuId" placeholder="请选择">
          <el-option
            v-for="item in spuList"
            :key="item.id"
            :label="item.spuName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="价格">
        从&nbsp;<el-input-number v-model="dataForm.minPrice" size="mini"></el-input-number>&nbsp;
        到&nbsp;<el-input-number v-model="dataForm.maxPrice" size="mini"></el-input-number>&nbsp;
      </el-form-item>

      <el-form-item label="sku 名字或 sku id">
        <el-input v-model="dataForm.key"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('pms:skuinfo:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('pms:skuinfo:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      :row-style="{height: '80px'}"
      class="elTableForSetRowHeight"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="skuId"
        header-align="center"
        align="center"
        label="skuId">
      </el-table-column>
      <el-table-column
        prop="spuId"
        header-align="center"
        align="center"
        label="spuId">
      </el-table-column>
      <el-table-column
        prop="skuName"
        header-align="center"
        align="center"
        label="sku名称">
      </el-table-column>
      <!--      <el-table-column
              prop="skuDesc"
              header-align="center"
              align="center"
              label="sku介绍描述">
            </el-table-column>-->
      <el-table-column
        prop="catalogId"
        header-align="center"
        align="center"
        label="所属分类id">
      </el-table-column>
      <el-table-column
        prop="brandId"
        header-align="center"
        align="center"
        label="品牌id">
      </el-table-column>
      <el-table-column
        prop="skuDefaultImg"
        header-align="center"
        align="center"
        label="默认图片">
        <template slot-scope="slot">
          <img
            :src="slot.row.skuDefaultImg.startsWith('20')?hostAddress+slot.row.skuDefaultImg:slot.row.skuDefaultImg"
            alt="" style="width: 100px" v-if="hostAddress &&slot.row.skuDefaultImg&&slot.row.skuDefaultImg!='null'">
        </template>
      </el-table-column>
      <el-table-column
        prop="skuTitle"
        header-align="center"
        align="center"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="skuSubtitle"
        header-align="center"
        align="center"
        label="副标题">
      </el-table-column>
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="价格">
      </el-table-column>
      <el-table-column
        prop="saleCount"
        header-align="center"
        align="center"
        label="销量">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.skuId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.skuId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import uploadToAiliOss from '../../../api/uploadToAiliOss'
import AddOrUpdate from './skuInfo/skuinfo-add-or-update'
import CategoryTreeCascader from '../common/categoryTreeCascader'
import brandApi from '../../../api/product/brandApi'
import 'default-passive-events'
import spuApi from '../../../api/product/spuApi'

export default {

  data () {
    return {
      dataForm: {
        brandId: null,
        catelogId: null,
        minPrice: null,
        maxPrice: null,
        key: null,
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      hostAddress: null,
      brandList: [],
      spuList:[],
    }
  },
  components: {
    CategoryTreeCascader,
    AddOrUpdate,
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      let {brandId, catelogId, key, minPrice, maxPrice,spuId} = this.dataForm

      let skuName

      let skuId

      if (/^[0-9]+$/.test(key)) {
        skuId = key

      } else if (key && key.length > 0) {
        skuName = key

      }

      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(`/pms/skuinfo/list/${this.pageIndex}/${this.pageSize}`),
        method: 'post',
        data: {
          brandId,
          catalogId:catelogId,
          skuName,
          skuId,
          spuId,
          minPrice: minPrice == 0 ? null : minPrice,
          maxPrice: maxPrice == 0 ? null : maxPrice
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.skuId
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/pms/skuinfo/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },

    async getHostAddress () {
      let p = await uploadToAiliOss.getHostAddress()
      this.hostAddress = p.data.host + '/'
    },
    async updateCategory (categoryId) {
      this.dataForm.catelogId = categoryId
      let p = await brandApi.getBrandByCategoryIdOrBrandName(categoryId)
      this.brandList = p.data.list;
      await this.updateSpuList(categoryId);
    },
    async updateSpuList(catalogId,currentIndex=1,size=10){
      let p = await spuApi.searchSpuByCondition(null,currentIndex,size,catalogId);

      this.spuList = p.data.page.list;

      console.log(this.spuList);

    },
  },
  created () {
    this.getHostAddress()
  }
}
</script>
<style>
.el-table__row {
  height: 200px;
  overflow: hidden;
}
</style>
